<?php
/**
 * Smart Library - Smart Framework
 * http://smart-framework.highet.eu
 *
 * @category   SmartL
 * @package    SmartL_Zend
 * @subpackage Cache
 * @copyright  Copyright (c) 2005-2008 Smart Framework team - Stepan Sindelar, Vojtech Vit
 * @license    http://www.opensource.org/licenses/bsd-license.php	New BSD License
 */

/**
 * @see SmartL_Zend_Cache_Wrapper
 */
require_once 'SmartL/Zend/Cache/Wrapper.php';

/**
 * Manager of SmartL cache adpaters.
 * They are stored here under their names.
 *
 * This class is not singleton, but it's main instance for application is stored in SmartL_Application singleton.
 * 
 * @package SmartL_Zend
 * @subpackage Cache
 */
class SmartL_Zend_Cache_Manager {
	/**
	 * Array of defined cache adapters.
	 *
	 * @var array[string name]=>SmartL_Zend_Cache_Wrapper
	 */
	private $_adapters = array ();
	/**
	 * Flag indicates if cache is disabled.
	 *
	 * @var boolean
	 */
	private $_cacheDisabled = false;
	/**
	 * Class contructor.
	 * 
	 * @param array $adapters Each item is added trought method $this->addAdapter($item,$name), value is used as first parameter and index as second.
	 */
	public function __construct(array $adapters = array()) {
		foreach ( $adapters as $key => $adapter ) {
			$this->addAdapter ( $key, $adapter );
		}
	}
	
	/**
	 * Sets if the whole cache is disabled.
	 * In other words if all adapters stored in this instance should be disabled. 
	 *
	 * @param boolean $flag Defaul is true.
	 */
	public function setCacheDisabled($flag = true) {
		$this->_cacheDisabled = $flag;
	}
	
	/**
	 * Returns adapter with given name.
	 *
	 * @param string $name
	 * @return Zend_Cache_Core
	 */
	public function getAdapter($name) {
		if (! isset ( $this->_adapters [$name] )) {
			require_once 'SmartL/Zend/Cache/Manager/Exception.php';
			throw new SmartL_Zend_Cache_Manager_Exception ( "Cache adapter with name {$name} doesn't exist." );
		}
		return $this->_adapters [$name];
	}
	
	/**
	 * Returns array of all adapters
	 *
	 * @return array[string adapterName]=>Zend_Cache_Core
	 */
	public function getAdapters() {
		return $this->_adapters;
	}
	
	/**
	 * Adds adapter with given options.
	 *
	 * @param string $name
	 * @param SmartL_Application_Config_CacheAdapterInfo|Zend_Cache_Core $adapter
	 * @param boolean $disabled Is this adapter disabled? Default: false.
	 * @return SmartL_Zend_Cache_CacheManager
	 */
	public function addAdapter($name, $adapter, $disabled = false) {
		if ($adapter instanceof SmartL_Application_Config_CacheAdapterInfo) {
			require_once 'Zend/Cache.php';
			$adapter = Zend_Cache::factory ( $adapter->getFrontendName (), $adapter->getBackendName (), $adapter->getFrontendOptions (), $adapter->getBackendOptions () );
		} elseif (! $adapter instanceof Zend_Cache_Core) {
			require_once 'SmartL/Zend/Cache/Manager/Exception.php';
			throw new SmartL_Zend_Cache_Manager_Exception ( "Cache adapter must be instance of Zend_Cache_Core or SmartL_Application_Config_CacheAdapterInfo." );
		}
		$this->_adapters [$name] = $this->_wrapAdapter ( $adapter, $disabled );
		return $this;
	}
	
	/**
	 * Removes adapter with given name
	 *
	 * @param string $name
	 * @return SmartL_Zend_Cache_CacheManager
	 */
	public function removeAdapter($name) {
		if (! isset ( $this->_adapters [$name] )) {
			require_once 'SmartL/Zend/Cache/Manager/Exception.php';
			throw new SmartL_Zend_Cache_Manager_Exception ( "Cache adapter with name {$name} doesn't exist." );
		}
		unset ( $this->_adapters [$name] );
		return $this;
	}
	
	/**
	 * Helper function for creating wrapper.
	 *
	 * @param Zend_Cache_Core $adapter
	 * @return SmartL_Zend_Cache_Wrapper
	 */
	private function _wrapAdapter($adapter, $disabled) {
		return new SmartL_Zend_Cache_Wrapper ( $adapter, $this->_cacheDisabled or $disabled );
	}
}